outcomes_standard <- outcomes <- c("absenteeism_action", # main table 4
																	 "repeat_absence", # Parents should act, table 5
																	 "village_action", # Community would intervene, table 5
																	 "abs_efficacy", # "Intervention is effective", table 5
																	 "abs_discussion", # "Discussed absenteeism", table 6
																	 "abs_goal", # "Teachers/absenteeism important", table 6
																	 "more_schools", # "Schools important", table 6
																	 "pta_frequency", # miscellaneous "PTA frequency", table D5
																	 "community_state", # miscellaneous, "Community fundraiser" table D5
																	 "gov_resp" # miscellaneous, "Parent’s responsibility" table D5
)


getPVal <- function(outcome, 
										treatment, 
										data, 
										covariates = NULL,
										hypothesis = "upr",
										resample_FE = TRUE,
										cluster_SE = TRUE){
	
	results <- ols_main(outcome = outcome,
											treatment = treatment,
											resample_FE = resample_FE,
											block_FE = TRUE,
											audience_size = TRUE,
											cluster_SE = cluster_SE,
											covariates = covariates,
											the_data = data,
											dosage = FALSE,
											dosage_indicator = NULL
	)

	if(hypothesis == "upr") {
	pval <- pt(results$fit_summary[treatment, "t value"], results$fit$df,lower.tail = F)
	}
	
	if(hypothesis == "lwr") {
		pval <- pt(results$fit_summary[treatment, "t value"], results$fit$df,lower.tail = T)
	}

	if(hypothesis == "two") {
		pval <- 2*pt(-abs(results$fit_summary[treatment, "t value"]), results$fit$df)
	
		}
	
	return(pval)
}


vht_village_level_el <- 
	vht_el %>% 
	group_by(tc_id,block_id,absenteeism) %>% 
	summarize(
		complain_absenteeism_el = mean(complain_absenteeism,na.rm = T),
		radius = unique(radius),
		n_end_mean = unique(n_end_mean)
	)
vht_village_level_el$tc <- vht_village_level_el$tc_id




getSortedPVals <- function(data, 
													 data_vht, 
													 adjustment = FALSE,
													 observed_rank_order = NULL){
	
	pvals_standard <- sapply(outcomes_standard, 
											getPVal, 
											treatment = "absenteeism", 
											data = subset(data, respondent_category == "Complier"),
											hypothesis = "upr")
	
	pval_candidate <- getPVal(outcome = "candidate",
														 treatment = "absenteeism",
														 data = subset(data, respondent_category == "Complier"),
														 covariates = c("name_educ","mr_mrs_educ","non_educ_policy"),
														 hypothesis = "upr")
	
	names(pval_candidate) <- "candidate"
	
	pval_absent_frequency <- getPVal(outcome = "absent_frequency",
														treatment = "absenteeism",
														data = subset(data, respondent_category == "Complier"),
														hypothesis = "two")
	
	names(pval_absent_frequency) <- "absent_frequency"
	
	
	pval_complain_absenteeism_el <- getPVal(outcome = "complain_absenteeism_el",
																					treatment = "absenteeism",
																					data = data_vht,
																					hypothesis = "two",
																					resample_FE = F,
																					cluster_SE = F)
	
	names(pval_complain_absenteeism_el) <- "complain_absenteeism_el"
	
	pvals <- c(pvals_standard, 
						 pval_candidate, 
						 pval_absent_frequency,
						 pval_complain_absenteeism_el)
	
	pvals <- sort(pvals,decreasing = F)
	
	if(adjustment){
		pvals <- pvals[observed_rank_order]
		for(i in 1:length(pvals)) {
			pvals[i] <- min(pvals[i:length(pvals)])
		}
	}
	
	
	return(pvals)
	
}


obs <- getSortedPVals(data = el,
											data_vht = vht_village_level_el)


observed_rank_order <- names(obs)

simulations <- replicate(n = sims_mc, expr = {
	new_data <- rerandomize()
	village_level <- new_data %>% 
		dplyr::group_by(tc_id) %>%
		dplyr::summarise(absenteeism = unique(absenteeism))
	new_data_vht <- vht_village_level_el
	new_data_vht$absenteeism <- NULL
	new_data_vht <- merge(new_data_vht, village_level[, c("tc_id", "absenteeism")], by = "tc_id")
	
	getSortedPVals(data = new_data,
								 data_vht = new_data_vht,
								 adjustment = T,
								 observed_rank_order = observed_rank_order)},simplify = T)

pvals <- rowMeans(simulations < obs)
names(pvals) <- names(obs)
original <- pvals

for(i in 1:length(pvals)) {
	pvals[i] <- min(pvals[i:length(pvals)])
}



table_data <- data.frame(original_pvals = obs, 
												 adjusted_pvals = pvals)
# DOUBLE CHECK THAT THESE LABELS ARE CORRECT IF YOU CHANGE SOMETHING - ORDER MAY CHANGE. 
rownames(table_data)  <- c("Conative attitudes ABS",
													 "Teachers/absenteeism important",
													 "Parents complain",
													 "Parents should act",
													 "Community would intervene",
													 "Schools important",
													 "PTA frequency",
													 "Community fundraiser",
													 "Discussed absenteeism",
													 "Reported absenteeism",
													 "Candidate platform",
													 "Intervention is effective",
													 "Parent's responsibility")

sink("03_tables/pvals_multiple_comparisons_absenteeism.tex")
print(kable(table_data, 
			col.names = c("Unadjusted parametric $p$-value",
										"Adjusted $p$-value"), 
			format = "latex",
			escape = F,
			digits = 3,align = c("c","c")))
sink()


